/****************************************************************************
*                                                                           *
* winMain.cpp -- WINDOW MAIN KERNEL ENGINE                                  *
*                                                                           *
* Copyright (c) UBISOFT-ACE-CRAIOVA. All rights reserved(2009-2010).        *
*																			*
* Programmer: Ionescu Marius, Oprescu Bogdan                                *
*                                                                           *
* Designer: Frunza Alin, Ionescu Marius                                     *
*                                                                           *
****************************************************************************/

#include <windows.h>
#include <commctrl.h>
#include ".\dxGameMgr.h"
#include ".\Common\dxDirectives.h"
#include ".\Common\dxTypes.h"
#include ".\dxUtility.h"
#include "resource.h"

HINSTANCE hInst;
HWND hWnd;
dxGameMgr *game=0;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
bool initWindow(HINSTANCE hInstance);
void LoadFile(HWND hWnd);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	if (!initWindow(hInstance))
	{
		MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
		return false;
	}
	
	game = new dxGameMgr();
	if (!game->init(hWnd, hInstance))
	{
		delete game;
		return 0;
	}

	MSG msg;
	ZeroMemory( &msg, sizeof(msg) );
	while( msg.message!=WM_QUIT )
	{
	   if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
	   {
		  TranslateMessage( &msg );
		  DispatchMessage( &msg );
	   }
	   /*else if(KEY_DOWN(VK_ESCAPE))
	   {
	       PostMessage(hWnd, WM_DESTROY, 0, 0);
	   }*/
	   else
	   {
			if(!game->getSleep())
				game->update();
	   }  
	}

	if (game)
	{
		delete game;
	}

	return (int)msg.wParam;
}

bool initWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;
	wcex.cbSize = sizeof(WNDCLASSEX); 
	wcex.style= 0;//CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc= (WNDPROC)WndProc;
	wcex.cbClsExtra= 0;
	wcex.cbWndExtra= 0;
	wcex.hInstance= hInstance;
	wcex.hIcon= 0;
	wcex.hCursor= LoadCursor(hInstance,MAKEINTRESOURCE(IDC_MYCURSOR)); 
	wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName= 0;
	wcex.lpszClassName= "MainWindowClass";
	wcex.hIconSm= 0;
		
	RegisterClassEx(&wcex);

	RECT rc;
	SetRect( &rc, 0, 0, SCREEN_WIDTH,SCREEN_HEIGHT );  
	AdjustWindowRectEx( &rc, WS_OVERLAPPEDWINDOW,false,0);  

	/*HWND hWnd = CreateWindow("MainWindowClass", 
		"DX ENGINE v.2", 
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 
		CW_USEDEFAULT, 
		rc.right-rc.left, 
		rc.bottom-rc.top, 
		NULL, NULL, 
		hInstance, NULL
	);*/
	
	DWORD style=WS_POPUP|WS_VISIBLE;
	 if(FULLSCREEN == 0)
		style = WS_OVERLAPPEDWINDOW;
	
	hWnd = CreateWindowEx(NULL,
                          "MainWindowClass",
                          "DX ENGINE v.2",
                          style,
                          CW_USEDEFAULT, 
						  CW_USEDEFAULT,
                          SCREEN_WIDTH,//SCREEN_WIDTH,//rc.right-rc.left 
						  SCREEN_HEIGHT,//SCREEN_HEIGHT,//rc.bottom-rc.top 
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

	if (!hWnd)
	{
      return false;
	}
 
   hInst = hInstance;
   ShowWindow(hWnd, SW_SHOW);
   UpdateWindow(hWnd);

   return true;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
	case WM_KEYDOWN:
		switch(wParam)
		{			
			// Animation Controls			
			case VK_SPACE:
				game->NextAnimationX();
			break;
			//case VK_ADD:
			//	game->AnimateFaster();
			//break;
			//case VK_SUBTRACT:
			//	game->AnimateSlower();
			//break;

			// Other keys
			case 'O':
				LoadFile(hWnd);
			break;
			case VK_F1:
				game->ToggleControlsDisplay();
			break;

			default:
				break;
		}
		break;
		/*case WM_ACTIVATE:
			if(wParam != WA_INACTIVE)
				game->m_bSleep = FALSE;
			else
				game->m_bSleep = TRUE;
		break;*/
        case WM_DESTROY:
             PostQuitMessage(0);
        /*case WM_KEYDOWN:
        {
            MessageBox(NULL,
           "Hello World!",
           "Just another Hello World program!",
           MB_ICONEXCLAMATION | MB_OK);
        } break;*/
			 // this message is read when the window needs refreshing
        /*case WM_PAINT:
		{
			// there are some variables needed, new types of variables
			HDC hDC;
			PAINTSTRUCT PaintStruct;
			RECT Location;

			// prepare the window for drawing and get the device context
			hDC = BeginPaint(hWnd, &PaintStruct);

			// fill our RECT with the size of the window contents
			GetClientRect(hWnd, &Location);

			// set the text background to TRANSPARENT, as the default is OPAQUE
			SetBkMode(hDC, TRANSPARENT);

			// draw the text to the window
			DrawText(hDC,
					 "This my game...",
					 -1,
					 &Location,
					 DT_SINGLELINE | DT_CENTER | DT_VCENTER);

			// let Windows know we are done drawing
			EndPaint(hWnd, &PaintStruct);

			return 0;
		} break;*/
        break;
       
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

void LoadFile(HWND hWnd)
{
    static char filename[4096];

    OPENFILENAME ofn;
    memset( &ofn, 0, sizeof(ofn) );

    ofn.lStructSize       = sizeof(ofn);
    ofn.hwndOwner         = hWnd;
    ofn.hInstance         = 0;
	ofn.lpstrFilter		  = "X Files (*.x)\0*.x\0All Files (*.*)\0*.*\0\0";
    ofn.lpstrCustomFilter = NULL;
    ofn.nMaxCustFilter    = 0L;
    ofn.nFilterIndex      = 0L;
    ofn.lpstrFile         = filename;
    ofn.nMaxFile          = sizeof(filename);
    ofn.lpstrFileTitle    = 0;
    ofn.nMaxFileTitle     = 0;
	ofn.lpstrInitialDir   = 0;
    ofn.nFileOffset       = 0;
    ofn.nFileExtension    = 0;
    ofn.lpstrDefExt       = "*.x";
    ofn.lCustData         = 0;
	ofn.lpstrTitle		  = "Select a .X file to open";
    ofn.Flags			  = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_OVERWRITEPROMPT;

	if (GetOpenFileName(&ofn))
		game->loadFileX(filename);
}